package org.jeecg.common.util.dingtalk;

import java.util.ArrayList;
import java.util.List;

import org.jeecg.common.constant.ProcessConstant;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGetJsapiTicketRequest;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiInactiveUserGetRequest;
import com.dingtalk.api.request.OapiInactiveUserV2GetRequest;
import com.dingtalk.api.request.OapiProcessGettodonumRequest;
import com.dingtalk.api.request.OapiProcessListbyuseridRequest;
import com.dingtalk.api.request.OapiProcessinstanceCreateRequest;
import com.dingtalk.api.request.OapiProcessinstanceCreateRequest.FormComponentValueVo;
import com.dingtalk.api.request.OapiProcessinstanceCreateRequest.ProcessInstanceApproverVo;
import com.dingtalk.api.request.OapiProcessinstanceListidsRequest;
import com.dingtalk.api.request.OapiUserCreateRequest;
import com.dingtalk.api.response.OapiGetJsapiTicketResponse;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiInactiveUserGetResponse;
import com.dingtalk.api.response.OapiInactiveUserV2GetResponse;
import com.dingtalk.api.response.OapiProcessGettodonumResponse;
import com.dingtalk.api.response.OapiProcessListbyuseridResponse;
import com.dingtalk.api.response.OapiProcessinstanceCreateResponse;
import com.dingtalk.api.response.OapiProcessinstanceListidsResponse;
import com.dingtalk.api.response.OapiUserCreateResponse;
import com.taobao.api.ApiException;

import me.kagura.JSONQuery;
import me.kagura.JsonResult;
import me.kagura.exception.FieldNotExistException;
import me.kagura.exception.TypeNotMismatchException;

/**
 * @description 钉钉API调用工具
 * @author heckerstone
 * @date 2020-07-24
 *
 */
public class DingTalkUtils {
	/**
	 * @descripton 获取钉钉接口token，该token在有效期内多次调用返回相同结果，并自动续期。如下方式直接请求完全ojbk
	 * @author heckerstone
	 * @date 2020-07-24
	 */
	public static String get_token() {
		String token="";
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_GETTOKEN);
			OapiGettokenRequest req = new OapiGettokenRequest();
			req.setAppkey(ProcessConstant.APPKEY);
			req.setAppsecret(ProcessConstant.APPSECRET);
			req.setHttpMethod("GET");
			OapiGettokenResponse rsp = client.execute(req);
			token=rsp.getBody();
			//开始解析返回结果json数据
			JsonResult jsonResult = JSONQuery.select(token, "access_token");
			token=jsonResult.toString();
		} catch (ApiException e) {
			token="";
		} catch (TypeNotMismatchException e) {
			token="";
		} catch (FieldNotExistException e) {
			e.printStackTrace();
		}
		return token;
	}
	
	/**
	 * @description 获取jsapi ticket
	 * @author heckerstone
	 * @date 2020-07-24
	 * @param access_token
	 */
	public static void get_jsapi_ticket(String access_token) {
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_GETJSAPITICKET);
			OapiGetJsapiTicketRequest req = new OapiGetJsapiTicketRequest();
			req.setHttpMethod("GET");
			OapiGetJsapiTicketResponse rsp = client.execute(req, "20d3dd21b57c3d618e0b0935ac887c3f");
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	public static void create(DingUser du) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/create");
			OapiUserCreateRequest req = new OapiUserCreateRequest();
			req.setUserid("");
			req.setIsSenior(false);//
			req.setIsHide(false);
			req.setJobnumber("200811004004");
			req.setEmail("heckerstone@163.com");
			req.setRemark("自动添加");
			req.setWorkPlace("胜利街文化路");
			req.setTel("13053613483");
			req.setMobile("13053613483");
			req.setPosition("经理");
			req.setDepartment("11");
			req.setName("22");
			req.setExtattrString("33");
			req.setOrgEmail("44");
			req.setOrderInDeptsString("55");
			req.setHiredDate(66L);
			OapiUserCreateResponse rsp = client.execute(req, "20d3dd21b57c3d618e0b0935ac887c3f");
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @description 获取待我审批数量
	 * @param userid
	 */
	public static void gettodonum(String userid) {
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_GETTONUM);
			OapiProcessGettodonumRequest req = new OapiProcessGettodonumRequest();
			req.setUserid("111");
			OapiProcessGettodonumResponse rsp = client.execute(req, "20d3dd21b57c3d618e0b0935ac887c3f");
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据用户id获取可见审批模板列表
	 */
	public static void listbyuserid(String userid,long offset,long size,String access_token) {
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_LISTBYUSERID);
			OapiProcessListbyuseridRequest req = new OapiProcessListbyuseridRequest();
			req.setUserid("111");
			req.setOffset(0L);
			req.setSize(10L);
			OapiProcessListbyuseridResponse rsp = client.execute(req, "20d3dd21b57c3d618e0b0935ac887c3f");
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 发起审批实例
	 */
	public static void createActivity() {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/create");
			OapiProcessinstanceCreateRequest req = new OapiProcessinstanceCreateRequest();
			req.setAgentId(836958846L);
			req.setProcessCode("111");
			req.setOriginatorUserId("111");
			req.setDeptId(11L);
			req.setApprovers("11,22");
			req.setCcList("33,44");
			req.setCcPosition("55");
			List<FormComponentValueVo> list2 = new ArrayList<FormComponentValueVo>();
			FormComponentValueVo obj3 = new FormComponentValueVo();
			list2.add(obj3);
			obj3.setName("66");
			obj3.setValue("77");
			obj3.setExtValue("88");
			req.setFormComponentValues(list2);
			List<ProcessInstanceApproverVo> list5 = new ArrayList<ProcessInstanceApproverVo>();
			ProcessInstanceApproverVo obj6 = new ProcessInstanceApproverVo();
			list5.add(obj6);
			obj6.setTaskActionType("AND");
			obj6.setUserIds(new ArrayList<String>());//审批人userid列表，会签/或签列表长度必须大于1，非会签/或签列表长度只能为1
			req.setApproversV2(list5);
			OapiProcessinstanceCreateResponse rsp = client.execute(req, "20d3dd21b57c3d618e0b0935ac887c3f");
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @description ERP系统录入采购单时，自动同步到钉钉审批中
	 * @date 2020-07-25
	 * @author heckerstone
	 */
	public static void uploadCGD() {
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_CREATEPROCESS);
			OapiProcessinstanceCreateRequest req = new OapiProcessinstanceCreateRequest();
			req.setProcessCode("PROC-DC326BC5-F99D-4D3B-A8C9-FD3464CCCCD3");
			req.setOriginatorUserId("035065345723610525");
			req.setAgentId(836958846L);
			req.setDeptId(1L);
			List<FormComponentValueVo> list2 = new ArrayList<FormComponentValueVo>();
			//表单中每一个控件
			OapiProcessinstanceCreateRequest.FormComponentValueVo obj3 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
			obj3.setName("采购日期");
			obj3.setValue("2020-07-25");
			
			OapiProcessinstanceCreateRequest.FormComponentValueVo obj4 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
			obj4.setName("需求部门");
			obj4.setValue("中软科");
			
			OapiProcessinstanceCreateRequest.FormComponentValueVo obj5 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
			
			obj5.setName("经办人");
			obj5.setValue("035065345723610525");
			
			OapiProcessinstanceCreateRequest.FormComponentValueVo obj6 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
			
			obj6.setName("采购名称");
			obj6.setValue("原材料");
			
			OapiProcessinstanceCreateRequest.FormComponentValueVo obj7 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
			
			obj7.setName("采购数量");
			obj7.setValue("1000");
			
			OapiProcessinstanceCreateRequest.FormComponentValueVo obj8 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
			
			obj8.setName("金额（元）");
			obj8.setValue("100.00");
			
			OapiProcessinstanceCreateRequest.FormComponentValueVo obj9 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
			
			obj9.setName("用途说明");
			obj9.setValue("原材料库存不足，采购补货");
			
			OapiProcessinstanceCreateRequest.FormComponentValueVo obj10 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
			obj10.setName("供应商联系人");
			obj10.setValue("035065345723610525");
			
			list2.add(obj3);
			list2.add(obj4);
			list2.add(obj5);
			list2.add(obj6);
			list2.add(obj7);
			list2.add(obj8);
			list2.add(obj9);
			list2.add(obj10);
			
//			// 附件
//			OapiProcessinstanceCreateRequest.FormComponentValueVo attachmentComponent = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
//			JSONObject attachmentJson = new JSONObject();
//			attachmentJson.put("fileId", "6433971140");
//			attachmentJson.put("fileName", "2644.JPG");
//			attachmentJson.put("fileType", "jpg");
//			attachmentJson.put("spaceId", "1635477658");
//			attachmentJson.put("fileSize", "333");
//
//			JSONArray array = new JSONArray();
//			array.add(attachmentJson);
//			attachmentComponent.setValue(array.toJSONString());
//			attachmentComponent.setName("附件");
//			
//			list2.add(attachmentComponent);
			
			req.setFormComponentValues(list2);
			OapiProcessinstanceCreateResponse rsp = client.execute(req, "0d25123934663af48c8674edf9cf5641");
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 分页获取审批实例id列表 
	 */
	public static void listids() {
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_GETLISTIDS);
			OapiProcessinstanceListidsRequest req = new OapiProcessinstanceListidsRequest();
			req.setProcessCode("processCode");
			req.setStartTime(20200723L);
			req.setSize(10L);
			req.setCursor(0L);
			OapiProcessinstanceListidsResponse rsp = client.execute(req, "20d3dd21b57c3d618e0b0935ac887c3f");
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
//		uploadCGD();
		System.out.println(get_token());
	}

}
